import file from '@system.file';
import request from '@ohos.request';
import fetch from '@system.fetch';
import router from '@system.router';
import prompt from '@system.prompt';
import geolocation from '@system.geolocation';
import Service from '../../generated/TestService.js';
import dataStorage from '@ohos.data.storage'
import mediaLibrary from '@ohos.multimedia.mediaLibrary';
import featureAbility from '@ohos.ability.featureAbility';
import http from '@ohos.net.http';

export default {
    data: {
        userid:null,
        touserid:null,
        tousername:'悟',
        toheadshot:'/common/picture/initheadshot.png',
        ownarticles:[],
        ownvideos:[],
        ownfollows:[],
    },
    onInit(){

    },
    onShow(){
        if(this.touserid!==null){
            this.LoadOwnArticle()
            this.LoadOwnFollow()
            this.LoadOwnVideo()
        }
    },
    TurnBack(){
        router.back();
    },
    LoadOwnArticle(){
        let httpRequest= http.createHttp();
        httpRequest.request("http://101.35.82.76:9002/article/getMyArticle?userid="+this.touserid,
            {
                method: 'GET',
                readTimeout: 60000,
                connectTimeout: 60000
            },(err, data) => {
                if (!err) {
                    console.info('Result:' + data.result);
                    console.info('code:' + data.responseCode);
                    console.info('header:' + data.header);

                    var res = JSON.stringify(JSON.parse(data.result).data)
                    var articles = JSON.stringify(JSON.parse(res).articles)
                    this.ownarticles = JSON.parse(articles)

                } else {
                    console.info('error:' + err.data);
                    prompt.showToast({
                        message: 'Send Error'+ JSON.stringify(err)
                    })
                }
            });
    },
    LoadOwnFollow(){
        let httpRequest= http.createHttp();
        httpRequest.request("http://101.35.82.76:9002/follow/getMyFollow?userid="+this.touserid,
            {
                method: 'GET',
                readTimeout: 60000,
                connectTimeout: 60000
            },(err, data) => {
                if (!err) {
                    console.info('Result:' + data.result);
                    console.info('code:' + data.responseCode);
                    console.info('header:' + data.header);

                    var res = JSON.stringify(JSON.parse(data.result).data)
                    var follows = JSON.stringify(JSON.parse(res).follows)
                    this.ownfollows = JSON.parse(follows)

                } else {
                    console.info('error:' + err.data);
                    prompt.showToast({
                        message: 'Send Error'+ JSON.stringify(err)
                    })
                }
            });
    },
    LoadOwnVideo(){
        let httpRequest= http.createHttp();
        httpRequest.request("http://101.35.82.76:9002/file/getMyVideo?userid="+this.touserid,
            {
                method: 'GET',
                readTimeout: 60000,
                connectTimeout: 60000
            },(err, data) => {
                if (!err) {
                    console.info('Result:' + data.result);
                    console.info('code:' + data.responseCode);
                    console.info('header:' + data.header);

                    var res = JSON.stringify(JSON.parse(data.result).data)
                    var myvideos = JSON.stringify(JSON.parse(res).myvideos)
                    this.ownvideos = JSON.parse(myvideos)

                } else {
                    console.info('error:' + err.data);
                    prompt.showToast({
                        message: 'Send Error'+ JSON.stringify(err)
                    })
                }
            });
    },
    detailPage(e) {
        var articleid = e.target.dataSet.articleid
        console.info(articleid)
        var article;
        for (var i = 0; i < this.ownarticles.length; i++) {
            if(this.ownarticles[i].articleid === articleid){
                article = this.ownarticles[i]
            }
        }
        router.push({
            uri: "pages/articledetail/articledetail",
            params: {
                articleid:articleid,
                title:article.title,
                tags:article.tags,
                body:article.body,
                cover:article.cover,
                username:article.username,
            }
        });
    },
    tochatpage(){
        if(this.userid!==null){
            router.push({
                uri: "pages/chatpage/chatpage",
                params: {
                    userid:this.userid,
                    opuserid:this.touserid,
                    sessionname:this.tousername,
                    opheadshot:this.toheadshot,
                }
            });
        }
    },

}
